package com.example.preparedstatement.crud;

import com.example.bean.Customer;
import com.example.bean.Order;
import com.example.util.JDBCUtils;
import org.junit.Test;

import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.List;

// 使用PreparedStatement实现对不同表的通用查询操作
public class PreparedStatementQueryTest {

    // 通用查询单条记录
    public <T> T getInstance(Class<T> clazz, String sql, Object... args) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            // 获取连接
            connection = JDBCUtils.getConnection();

            // 编译SQL
            preparedStatement = connection.prepareStatement(sql);
            // 设置参数
            for (int i = 0; i < args.length; i++) {
                preparedStatement.setObject(i + 1, args[i]);
            }

            // 查询结果
            resultSet = preparedStatement.executeQuery();
            // 获取结果集元数据
            ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
            int columnCount = resultSetMetaData.getColumnCount();

            // 遍历结果集
            if (resultSet.next()) {
                T t = clazz.newInstance();
                for (int i = 0; i < columnCount; i++) {
                    Object columnValue = resultSet.getObject(i + 1);
                    // 获取列名
                    String columnName = resultSetMetaData.getColumnLabel(i + 1);

                    // 通过反射设置属性值
                    Field field = clazz.getDeclaredField(columnName);
                    field.setAccessible(true);
                    field.set(t, columnValue);
                }
                return t;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        } finally {
            // 释放资源
            JDBCUtils.closeResource(connection, preparedStatement, resultSet);
        }
        return null;
    }

    @Test
    public void testGetInstance() {
        String sql = "SELECT id, name, email, birth FROM customers WHERE id = ?";
        Customer customer = getInstance(Customer.class, sql, 2);
        System.out.println(customer);

        String sql2 = "SELECT order_id orderId, order_name orderName, order_date orderDate FROM `order` WHERE order_id = ?";
        Order order = getInstance(Order.class, sql2, 2);
        System.out.println(order);
    }

    // 通用查询多条记录
    public <T> List<T> getForList(Class<T> clazz, String sql, Object... args) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            // 获取连接
            connection = JDBCUtils.getConnection();

            // 编译SQL
            preparedStatement = connection.prepareStatement(sql);
            // 设置参数
            for (int i = 0; i < args.length; i++) {
                preparedStatement.setObject(i + 1, args[i]);
            }

            // 查询结果
            resultSet = preparedStatement.executeQuery();
            // 获取结果集元数据
            ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
            int columnCount = resultSetMetaData.getColumnCount();

            // 遍历结果集
            List<T> list = new ArrayList<>();
            while (resultSet.next()) {
                T t = clazz.newInstance();
                for (int i = 0; i < columnCount; i++) {
                    Object columnValue = resultSet.getObject(i + 1);
                    // 获取列名
                    String columnName = resultSetMetaData.getColumnLabel(i + 1);

                    // 通过反射设置属性值
                    Field field = clazz.getDeclaredField(columnName);
                    field.setAccessible(true);
                    field.set(t, columnValue);
                }
                list.add(t);
            }
            return list;
        } catch (Exception exception) {
            exception.printStackTrace();
        } finally {
            // 释放资源
            JDBCUtils.closeResource(connection, preparedStatement, resultSet);
        }
        return null;
    }

    @Test
    public void testGetForList() {
        String sql = "SELECT id, name, email, birth FROM customers WHERE id < ?";
        List<Customer> customers = getForList(Customer.class, sql, 8);
        customers.forEach(System.out::println);

        String sql2 = "SELECT order_id orderId, order_name orderName, order_date orderDate FROM `order` WHERE order_id < ?";
        List<Order> orders = getForList(Order.class, sql2, 3);
        orders.forEach(System.out::println);
    }

}
